package spark.mllib

//import org.apache.spark.mllib.linalg.distributed.RowMatrix
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.SparkSession
import org.apache.spark.{SparkConf, SparkContext}

/**
  * Created by liuwei on 2017/7/13.
  */
object SVDTest {

  def main(args: Array[String]): Unit = {
    import org.apache.spark.mllib.linalg.Matrix
    import org.apache.spark.mllib.linalg.SingularValueDecomposition
    import org.apache.spark.mllib.linalg.Vector
    import org.apache.spark.mllib.linalg.Vectors
    import org.apache.spark.mllib.linalg.distributed.RowMatrix

    var arr1:Array[Double] = new Array[Double](10000)
    for(i <- 0 to arr1.length-1){
      arr1(i) = i%10
    }
//    arr1.foreach(println)

    val v1 =Vectors.dense(arr1);

    val sparkConf = new SparkConf().setAppName("PCATest").setMaster("local[8]")
    val sc = new SparkContext(sparkConf)
    val data = Array(
      Vectors.sparse(5, Seq((1, 1.0), (3, 7.0))),
      Vectors.dense(2.0, 0.0, 3.0, 4.0, 5.0),
      Vectors.dense(4.0, 0.0, 0.0, 6.0, 7.0))

    val rows = sc.parallelize(data)

    val mat: RowMatrix = new RowMatrix(rows)

    // Compute the top 5 singular values and corresponding singular vectors.
    val svd: SingularValueDecomposition[RowMatrix, Matrix] = mat.computeSVD(5, computeU = true)
    // U右奇异矩阵
    val U: RowMatrix = svd.U  // The U factor is a RowMatrix.
    U.rows.foreach(println)
    println("===============")
    // s奇异值向量
    val s: Vector = svd.s     // The singular values are stored in a local dense vector.
    println(s)
    println("===============")
    // V左奇异矩阵
    val V: Matrix = svd.V     // The V factor is a local dense matrix.
    V.transpose
    println(V)
  }

}
